package com.example.dao;

import java.util.List;
import java.util.Map;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.example.domain.Recommend;

@Repository
public interface RecommendDao extends JpaRepository<Recommend, Integer> {

    @Transactional
    @Modifying
    Integer deleteByRecommendId(Integer recommendId);

    List<Recommend> findByRecommendId(Integer recommendId);

    @Transactional
    @Modifying
    Integer deleteByRecommendIdIn(List<Integer> listId);

    @Query(nativeQuery = true, value = "SELECT u.user_Id as userId,u.account as userName,u.nickName,r.*\r\n" +
            "FROM users u INNER JOIN recommend r ON u.user_Id = r.student_Id")
    List<Map<String, Object>> queryAllRecommendAndUsersInfo();

    @Query(nativeQuery = true, value = "SELECT u.user_Id as userId,u.account as userName,u.nickName,r.*\r\n" +
            "FROM users u INNER JOIN recommend r ON u.user_Id = r.student_Id\r\n" +
            "where r.enterpriseName LIKE CONCAT('%',:enterpriseName,'%')")
    List<Map<String, Object>> findByEnterpriseNameLike(@Param("enterpriseName") String enterpriseName);

    @Query(nativeQuery = true, value = "SELECT u.user_Id as userId,u.account as userName,u.nickName,r.*\r\n" +
            "FROM users u INNER JOIN recommend r ON u.user_Id = r.student_Id\r\n" +
            "where r.enterpriseTelphone LIKE CONCAT('%',:enterpriseTelphone,'%')")
    List<Map<String, Object>> findByEnterpriseTelphoneLike(@Param("enterpriseTelphone") String enterpriseTelphone);

    @Query(nativeQuery = true, value = "SELECT u.user_Id as userId,u.account as userName,u.nickName,r.*\r\n" +
            "FROM users u INNER JOIN recommend r ON u.user_Id = r.student_Id\r\n" +
            "where r.referrerName LIKE CONCAT('%',:referrerName,'%')")
    List<Map<String, Object>> findByReferrerNameLike(@Param("referrerName") String referrerName);

    @Query(nativeQuery = true, value = "SELECT u.user_Id as userId,u.account as userName,u.nickName,r.*\r\n" +
            "FROM users u INNER JOIN recommend r ON u.user_Id = r.student_Id\r\n" +
            "where r.status LIKE CONCAT('%',:status,'%')")
    List<Map<String, Object>> findByStatusLike(@Param("status") String status);

    @Query(nativeQuery = true, value = "SELECT u.user_Id as userId,u.account as userName,u.nickName,r.*\r\n" +
            "FROM users u INNER JOIN recommend r ON u.user_Id = r.student_Id\r\n" +
            "where u.account LIKE CONCAT('%',:userName,'%')")
    List<Map<String, Object>> findByUserNameLike(@Param("userName") String userName);

    @Query(nativeQuery = true, value = "SELECT u.user_Id as userId,u.account as userName,u.nickName,r.*\r\n" +
            "FROM users u INNER JOIN recommend r ON u.user_Id = r.student_Id\r\n" +
            "where u.nickName LIKE CONCAT('%',:nickName,'%')")
    List<Map<String, Object>> findByNickNameLike(@Param("nickName") String nickName);


}
